package VacationTest.PlaneFight.src;

import VacationTest.PlaneFight.src.Gameover.MyFrame;

import javax.swing.*;
import java.awt.*;

public class EnemyPlane extends Thread {

    public GameFrame gf;
    // 敌机的位置 大小 速度
    int x, y;
    int width = 50, height = 50;
    int speed = 3;
    Image img = new ImageIcon(
            "E:\\JavaCode\\AirWar\\src\\VacationTest\\PlaneFight\\Image\\EmenyPlane.png")
            .getImage();

    // 构造器
    public EnemyPlane(int x, int y, GameFrame gf) {
        super();
        this.x = x;
        this.y = y;
        this.gf = gf;
    }

    // 构造器
    public EnemyPlane(int x, int y, int width, int height, GameFrame gf) {
        super();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.gf = gf;
    }

    // 重写run方法 在线程启动时 执行
    public void run() {
        while (true) {
            if (hit()) {
                // System.out.println("hit.............");
                // 敌机被击毁后，速度变为0 图片更换为 爆炸图片
                this.speed = 0;
                this.img = new ImageIcon(
                        "E:\\JavaCode\\AirWar\\src\\VacationTest\\PlaneFight\\Image\\boom3.png")
                        .getImage();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 子弹击毁 移除敌机
                gf.enemys.remove(this);
                break;
            }
            // 敌机超出下边界 移除敌机
            if (this.y > 760) {
                gf.enemys.remove(this);
                break;
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (crash()) {
                this.speed = 0;
                this.img = new ImageIcon().getImage();
                new MyFrame();	//实例化MyFrame对象
                gf.enemys.remove(this);
                gf.heroplane.img = new ImageIcon(
                        "E:\\JavaCode\\AirWar\\src\\VacationTest\\PlaneFight\\Image\\HeroPlaneBoom.png")
                        .getImage();
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //gf.heroplane.addActionListener(new broken());
                System.exit(0);
            }

        }
    }
    // 检测碰撞
    public boolean hit() {
        // Swing技术中，已经实现相关算法
        // 给出敌机的矩形
        Rectangle myrect = new Rectangle(this.x, this.y, this.width, this.height);
        Rectangle rect = null;// 给出子弹的矩形
        for (int i = 0; i < gf.bullets.size(); i++) {
            bullet b = gf.bullets.get(i);
            rect = new Rectangle(b.x, b.y, b.width, b.height);
            // 碰撞检测
            if (myrect.intersects(rect)) {
                return true;
            }
        }
        return false;

    }

    // 检测 英雄机和 敌机碰撞
    public boolean crash() {
        Rectangle myrect = new Rectangle(this.x, this.y, this.width, this.height);
        Rectangle rect2 = new Rectangle(gf.heroplane.x, gf.heroplane.y, gf.heroplane.width, gf.heroplane.height);
        if (myrect.intersects(rect2)) {
            return true;
        }
        return false;
    }

}

